package com.nerve.cloudoffice.web.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.nerve.util.RequestUtil;

import com.nerve.cloudoffice.common.Const;
import com.nerve.cloudoffice.common.Reply;
import com.nerve.cloudoffice.common.SessionUtil;
import com.nerve.cloudoffice.domain.Department;
import com.nerve.cloudoffice.domain.User;
import com.nerve.cloudoffice.domain.dao.DepartmentDao;
import com.nerve.cloudoffice.service.UserService;
import com.nerve.cloudoffice.service.impl.UserServiceImpl;
import com.nerve.cloudoffice.web.model.IndexModel;

/**
 * @project cloudOffice IndexAction.java
 * @datetime Jul 29, 2013 3:10:20 AM
 * @description 
 *    首页
 * @author 集成显卡 IBM 
 *			zxingming@qq.com
 *			zengxm@cn.ibm.com
 *
 * @change log:
 *
 */
@SuppressWarnings("serial")
public class IndexAction extends NerveAction{

	private String name;
	private String password;
	
	private UserService service = new UserServiceImpl();
	
	@Override
	public String execute() throws Exception {
		DepartmentDao dao = new DepartmentDao();
		System.out.println(dao);
		Department d = dao.getById(1);
		System.out.println("get id =" + d.getName());
		print("首页调用正常," + d.getName());
		return null;
	}
	
	/**
	 * @method name: login
	 * @description: 用户登录
	 * @return type: String
	 *	@return
	 */
	public String login(){
		HttpServletRequest request = ServletActionContext.getRequest();
		if(request.getMethod().toUpperCase().equals("GET")){
			return ERROR;
		}
		try{
			User user = service.login(name, password);
			int app = RequestUtil.getInt(request, "app", 0);
			
			//app的应答方式
			if(app == 1){
				Reply re = new Reply();
				if(user == null){
					re.error(new Exception("帐号或者密码不正确！"));
				}else{
					SessionUtil.put(request, Const.LOGIN_USER, user);
					re.setMessage(user);
				}
				
				json(re);
				return null;
			}
			//网页的应答方式
			else{
				if(user == null){
					request.setAttribute(Const.ERROR, "帐号或者密码不正确！");
					return ERROR;
				}else{
					System.out.println("login ok!!!!");
					SessionUtil.put(request, Const.LOGIN_USER, user);
					
					ServletActionContext.getResponse().sendRedirect("index.jsp");
					return null;
				}
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return ERROR;
	}
	
	
	/**
	 * @method name: getNotify
	 * @description: 获取首页的信息，这个会返回有多少通知公告没有查看，有多少任务没有完成
	 * @return type: void
	 */
	public void getNotify(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Reply re = new Reply();
		
		int debug = RequestUtil.getInt(request, "app", 0);
		
		try{
			int notifyId = RequestUtil.getInt(request, "notifyId", 0);
			IndexModel model = new IndexModel();
			model.setLastNotifyId(notifyId);
			
			Map<String, Integer> map = service.countNotify(model);
			
			re.setMessage(map);
		}catch(Exception e){
			re.error(e);
			e.printStackTrace();
			if(debug == 1){
				try{
					e.printStackTrace(ServletActionContext.getResponse().getWriter());
				}catch(Exception we){
					
				}
			}
		}
		if(debug == 0)
			json(re);
		
	}
	
	
	/**
	 * @method name: logout
	 * @description: 注销登录
	 * @return type: String
	 *	@return
	 */
	public String logout(){
		SessionUtil.clearUser(ServletActionContext.getRequest());
		return SUCCESS;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}